package com.zw.pdm.module.production.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.MakeChange;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.production.repository.MakeResultRepository;
import com.zw.pdm.module.production.service.ProductionQueryService;
import com.zw.pdm.module.production.service.ProductionSheetService;
import com.zw.pdm.module.report.service.ReportQueryService;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.service.DeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_CHANGE_REASON;
import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionQueryController.java
 * @Description TODO 制作件清单查询
 * @createTime 2020年10月06日 16:43:00
 */
@Controller
@RequestMapping("/production/query")
@SuppressWarnings("all")
public class ProductionQueryController extends BaseController {


    @Autowired
    private ProductionQueryService productionQueryService;
    @Autowired
    private ProductionSheetService productionSheetService;
    @Autowired
    private ReportQueryService reportQueryService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MakeResultRepository makeResultRepository;

    @GetMapping
    @RequiresPermissions(value = {"production:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public String page(String proCode,Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("depts", deptService.findAllTopLevelDept());
        model.addAttribute("proCode", proCode);
        return "production/query/makeQuery";
    }

    /** 计划列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions(value = {"production:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public JsonResult list(MakeSheet makeSheet) {
        startPage();
        List<MakeSheet> list = productionQueryService.findWithReportQuery(makeSheet);
        return jsonResult(list);
    }


    /** 制作件列表查看详细的制作件页面页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions(value = {"production:query:view", "dsn:prodetQuer:view"}, logical = Logical.OR)
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
        model.addAttribute("makeSheet", makeSheet);
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/query/makeInfo";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            return "production/query/makeChangeInfo";
        }
    }

    /** 查看审核过后的所有数据*/
    @GetMapping("/selectAll")
    @RequiresPermissions(value = {"production:query:view", "dsn:prodetQuer:view","production:list:view"}, logical = Logical.OR)
    public String selectAll(String proCode,String special,String part, String time1,String time2,String time3, Model model){
        model.addAttribute("selectOne", makeResultRepository.findByProCodeAndSpecialAndPartAndIsDelOrderByOrderNum(proCode,special,part,0).get(0));
        model.addAttribute("selectAll", makeResultRepository.findByProCodeAndSpecialAndPartAndIsDelOrderByOrderNum(proCode,special,part,0));
        model.addAttribute("time1",time1);
        model.addAttribute("time2",time2);
        model.addAttribute("time3",time3);
        return "production/query/makeSelectAll";
    }








    /** 计划列表 */
    @ResponseBody
    @PostMapping("/plan/list")
    @RequiresPermissions({"production:query:view"})
    public JsonResult planList(MakePlan makePlan) {
        startPage();
        List<MakePlan> list = productionQueryService.findPlanDetailList(makePlan);
        return jsonResult(list);
    }

    /** 变更列表 */
    @ResponseBody
    @PostMapping("/change/list")
    @RequiresPermissions({"production:query:view"})
    public JsonResult planList(MakeChange change) {
        startPage();
        List<MakeChange> list = productionQueryService.findChangeDetailList(change);
        return jsonResult(list);
    }

//    /** 查看打印 */
//    @GetMapping("/print/{id}")
//    @RequiresPermissions({"production:query:print"})
//    public String print(@PathVariable("id") Integer sheetId, Model model) {
//        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
//        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
//        model.addAttribute("MakeSheet", makeSheet);
//        if (makeSheet.getSheetType() == 1) {
//            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
//            return "production/list/makePrint";
//        } else {
//            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
//            model.addAttribute("changes", productionSheetService.findChangeBySheetId(sheetId));
//            return "production/list/makeChangePrint";
//        }
//    }

    /**************************************************变更后的需求************************************************************************/

    /** 查看打印 */
    @GetMapping("/print/{id}")
    @RequiresPermissions({"production:query:view"})
    public String print(@PathVariable("id") Integer sheetId, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
        model.addAttribute("MakeSheet", makeSheet);
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/query/makeQueryPrint";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            return "production/query/makeQueryChangePrint";
        }
    }


}
